﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;
using Ecopsy.DataTier;
using Ecopsy.Tools;

public partial class AgencyTree : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) MyDataBind();
        

    }

    private void MyDataBind()
    {
        lvAgencyTree.DataSource = GetTreeSource();
        lvAgencyTree.DataBind();

        lvDictionary.Nodes.Clear();
        TreeNode nd = new TreeNode ();
        nd.Text = "<a class=TreeNode ID=SysDictionary_  href=\"#\"  onclick=\"showTreeBranch('iFarmeAdmin_DictionaryList.aspx?AgencyID=','SysDictionary_');\">  Справочники провайдера  </a>";
        lvDictionary.Nodes.Add(nd);
  
        

        
    }
    #region Источник для дерева  Agency_TreeDataSource

    private F_TreeDataSource GetTreeSource()
    {
        ///Получить дерево департаментов
        AgencyTreeBox l = new AgencyTreeBox();
        l.Load();
        TreeEntitySingle tree = (TreeEntitySingle)l;


        // определить делегат поставляющий класс определяющий формат ноды в дереве
        Func<TreeEntitySingle, F_TreeDataSource.F_TreeDataSourceView.F_TreeHierarchyData> f = delegate(TreeEntitySingle _tr)
        {
            return (AgencyNodeItemFormatter)new AgencyNodeItemFormatter(_tr);
        };

        // вернуть древовидныйисточник данных
        return new F_TreeDataSource(tree, f);
    }
    public class AgencyNodeItemFormatter : F_TreeDataSource.F_TreeDataSourceView.F_TreeHierarchyData
    {
        public AgencyNodeItemFormatter(TreeEntitySingle obj) : base(obj) { }

        public override bool HasChildren
        {
            get
            {
                return true;
            }
        }

        public override F_TreeDataSource.F_TreeDataSourceView.F_TreeHierarchyData GetNewObj(TreeEntitySingle nd)
        {
            return new AgencyNodeItemFormatter(nd);
        }
        public override IHierarchicalEnumerable GetChildren()
        {
            Ecopsy.Tools.F_TreeDataSource.F_TreeDataSourceView.TreeHierarchicalEnumerable children = new F_TreeDataSource.F_TreeDataSourceView.TreeHierarchicalEnumerable();
            {
                string sid = _nd.ID.Replace("-", string.Empty);
                AgencyTreeBox a = (AgencyTreeBox)_nd;                
                TreeNode nn = new TreeNode();
                nn = new TreeNode();
                string sUrlKey = "iFarmeAdmin_SiteMap";
                if (((AgencyTreeBox)_nd).Agency != null)
                {
                    nn.Text = "<a class=TreeNode ID=Agency_Empl_" + _nd.ID + " href=\"#\"  onclick=\"showTreeBranch('" + sUrlKey + ".aspx?AgencyID=" + a.Agency.ID + "','Agency_Empl_" + _nd.ID + "');\">  Карта сайта  </a>";
                    children.Add(new TreeNodeHierarchyData(nn));

                    TreeNode nn1 = new TreeNode();
                    nn1.Text = "<a class=TreeNode ID=DictionaryList_" + _nd.ID + " href=\"#\"  onclick=\"showTreeBranch('" + "iFarmeAdmin_DictionaryList" + ".aspx?AgencyID=" + a.Agency.ID + "','DictionaryList_" + _nd.ID + "');\">  Справочники  </a>";
                    children.Add(new TreeNodeHierarchyData(nn1));
                }
                foreach (TreeEntitySingle n in _nd.ChildNodes)
                {
                    children.Add(GetNewObj(n));
                }
            }
            
            return children;
        }
        public override string ToString()
        {
            AgencyTreeBox a = (AgencyTreeBox)_nd;
            string sOnclick = string.Empty;
            if (a.Agency == null)
            {
                sOnclick = "onclick=\"showTreeBranch('AgencyEditor.aspx?DepartmentID=" + _nd.ID + "','Department_" + _nd.ID + "');";
                return "<a class=TreeNode ID=Department_" + _nd.ID + "href=\"#\" " + sOnclick + "  \"> " +  "Рекламные агентсва"  + " </a>";
            }
            else
            {
                sOnclick = "onclick=\"showTreeBranch('AgencyEditor.aspx?ID=" + a.Agency .ID  + "','Department_" + _nd.ID + "');";
                return "<a class=TreeNode ID=Department_" + _nd.ID + "href=\"#\" " + sOnclick + "  \"> " + (a.Agency == null ? "Рекламные агентсва" : a.Agency.Name) + " </a>";
            }
        }
        public override string Path
        {
            get
            {
                return _nd.ID;
            }
        }
        public override object Item
        {
            get
            {
                return _nd;
            }
        }


    }
    public class TreeNodeHierarchyData : IHierarchyData
    {

        public TreeNodeHierarchyData(TreeNode obj)
        {
            _nd = obj;
        }

        private TreeNode _nd = null;

        public virtual IHierarchicalEnumerable GetChildren()
        {
            Ecopsy.Tools.F_TreeDataSource.F_TreeDataSourceView.TreeHierarchicalEnumerable fshe = new F_TreeDataSource.F_TreeDataSourceView.TreeHierarchicalEnumerable();

            foreach (TreeNode n in _nd.ChildNodes)
            {
                fshe.Add(new TreeNodeHierarchyData(n));


            }
            return fshe;
        }

        public virtual IHierarchyData GetParent()
        {
            if (null == _nd.Parent) return null;
            return new TreeNodeHierarchyData(_nd.Parent);
        }
        
        public virtual bool HasChildren
        {
            get
            {
                //return true;
                return _nd.ChildNodes.Count > 0;
            }
        }

        public virtual string Path
        {
            get
            {
                return _nd.Value.ToString();
            }
        }

        public virtual object Item
        {
            get
            {
                return _nd;
            }
        }

        public virtual string Type
        {
            get
            {
                return "TreeNode";
            }
        }

        public override string ToString()
        {
            return _nd.Text;
        }


    }

    #endregion
}
